#!/usr/bin/env node

const http    = require('https'),
      cheerio = require('cheerio'),
      log     = console.log,
      print   = require('util').debuglog('dev'),
      addr    = 'https://ke.sifou.com/free';

http.get(addr, (res) => {
  let result = '';

  res.on('data', (data) => {
    result += data.toString('utf8');   
  });
  //log(result);
  res.on('end', () => {
    //print(result);

    let $ = cheerio.load(result);
	log($('body').find('.card-body').length);
    $('body').find('.card-body').each(function(){
      //print($(this).html());
      //log($(this).text());
      log();
      let cName = $(this).find('.card-title>a').text(),
          cURL  = $(this).find('.card-title>a').attr('href');

      cURL = 'https://segmentfault.com' + cURL;

      if(cName === '') return;

      log('课程名称：', cName);                                       
	  log('课程网址：', cURL.trim());
      //log('');
    });
  });
});
